<?php
/**
 * User: wanghousheng
 * Date: 2017/8/25
 * Time: 17:49
 */

class Clmsm
{

    private $smsapi = 'http://api.smsbao.com/';
    private $username = 'xckj2017';
    private $password = '123456';
    private $sign = "校创科技";//签名
//    private $url = "$this->smsapi.'sms?u='.$this->username.'&p='.$this->password.'&m='.$this->phone.'&c='.urlencode($content)";
    public function __construct($params = array())
    {
        if ($params['sign']) $this->sign = $params['sign'];
        if ($params['username']) $this->username = trim($params['username']);
        if ($params['password']) $this->password = trim($params['password']);
        if (empty($this->username) || empty($this->password)) {
            $this->ApiState(false, '配置信息错误');
        }
    }

    /**
     * Name: 发送短信
     * User: wanghousheng
     * Date: 2017/6/14
     * @param $mobile
     * @param $captcha
     * @param int $type
     * @param string $user_name
     * @param int $user_id
     */

    //    $smsapi = "http://api.smsbao.com/";
//$user = "***"; //短信平台帐号
//$pass = md5("****"); //短信平台密码
//$content="短信内容";//要发送的短信内容
//$phone = "*****";//要发送短信的手机号码
//$sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
//$result =file_get_contents($sendurl) ;
//echo $statusStr[$result];
    protected function send($mobile, $content)
    {
        $statusStr = array(
            "0" => "短信发送成功",
            "-1" => "参数不全",
            "-2" => "服务器空间不支持,请确认支持curl或者fsocket，联系您的空间商解决或者更换空间！",
            "30" => "密码错误",
            "40" => "账号不存在",
            "41" => "余额不足",
            "42" => "帐户已过期",
            "43" => "IP地址限制",
            "50" => "内容含有敏感词"
        );
        if ($mobile && $content) {
            $post_data = array(
                'account' => $this->username,
                'pswd' => md5($this->password),
                'msg' => trim($content),
                'mobile' => $mobile,
            );
            $sendUrl = $this->smsapi."sms?u=".$post_data['account']."&p=".$post_data['pswd']."&m=".$post_data['mobile']."&c=".urlencode($content);
            $result =file_get_contents($sendUrl) ;
            /*$string = '';
            foreach ($post_data as $k => $v) {
                $string .= "$k=" . urlencode($v) . '&';
            }
            $post_string = substr($string, 0, -1);
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_URL, $this->url);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //如果需要将结果直接返回到变量里，那加上这句。
            $result = curl_exec($ch);*/
//            $result = explode(',', $result);
//            var_export($result);die;
            if (intval($result) == 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * Name: 返回状态
     * User: wanghousheng
     * Date: 2017/6/14
     * @param bool $status
     * @param string $msg
     * @param array $data
     * @return array
     */
    protected function ApiState($status = true, $msg = 'success')
    {
        return array('status' => $status, 'message' => $msg);
    }


}